package com.ketangpai.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ketangpai.admin.dao.UserDao;
import com.ketangpai.admin.entity.Attachment;
import com.ketangpai.admin.entity.User;
import com.ketangpai.admin.entity.param.LoginParam;
import com.ketangpai.admin.exception.BusinessException;
import com.ketangpai.admin.service.MinioService;
import com.ketangpai.admin.service.UserService;
import com.ketangpai.admin.utils.Encrypt;
import com.ketangpai.admin.utils.JwtUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

@Service("userService")
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private MinioService minioService;
    /**
     * 登录
     */
    public User login(LoginParam param) throws Exception {
        // 对用户输入的密码进行加密
        String encodedPassword = Encrypt.AESEncrypt(param.getPassword());
//        System.out.println("密码"+Encrypt.AESDecrypt(param.getPassword()));
        User u = userDao.getByAccountAndPwd(param.getAccount(), encodedPassword);
        if (u != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("user", u);
            String jwt = JwtUtils.generateJWT(claims);
            System.out.println(jwt);
            claims.put("jwt", jwt);
            u.setToken(jwt);
            update(u);
            return u;
        } else {
            throw new BusinessException("用户名或密码有误！");
        }
    }
    /**
     * 注册
     */
    @Override
    public String register(User user) throws Exception {
        if (userDao.getByAccount(user.getAccount()) != null) {
            return "账户重复";
        }
        if (isValidPhoneNumber(user.getPhone())) {
           return  "请输入正确的手机号";
        }
        // 对用户输入的密码进行加密
        String encodedPassword = Encrypt.AESEncrypt(user.getPassword());
        user.setPassword(encodedPassword);
        return "注册成功";
    }
    /**
     * 手机号校验
     */
    public static boolean isValidPhoneNumber(String phoneNumber) {
        if ((phoneNumber != null) && (phoneNumber.length() == 11)) {
            return !Pattern.matches("^1[3-9]\\d{9}$", phoneNumber);
        }
        return true;
    }

    @Override
    public User selectById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    public User selectByAccount(String account) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("account",account);
        User user = userDao.selectOne(wrapper);
        if(user==null){
            throw new BusinessException("找不到用户");
        }
        return user;
    }

    @Override
    public boolean update(User user) {
        System.out.println(user);
//        if (user.getFile() != null){
//            Attachment attachment = minioService.storeFile(user.getFile());
//            user.setFileImage(attachment.getPath());
//        }
        return userDao.updateById(user) > 0;
    }

}
